﻿using AutoMapper;
using MediatR;
using SlaughterItem.API.Application.Commands.Week01.UsersCommands;
using SlaughterItem.Common.Domains.Manages;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week01.ManageCommands.Clientses
{
    /// <summary>
    /// 客户添加
    /// </summary>
    public class ClientsAddHandler : IRequestHandler<ClientsAddCommend, Result>
    {
        private readonly IBaseRepository<Clients> Clients;
        private readonly IMapper mapper;

        public ClientsAddHandler(IBaseRepository<Clients> Clients, IMapper mapper)
        {
            this.Clients = Clients;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(ClientsAddCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Clients clients = mapper.Map<Clients>(request);
                var res = Clients.Add(clients);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
